#include "viewwidget.h"

////////// ViewToolbarAction ///////////////////////////////////////////////////////////////////////

ToolBarItem::ToolBarItem(QAction * action, QObject * parent)
    : QObject(parent)
{
    m_action = action;
}

QAction * ToolBarItem::action()
{
    return m_action;
}

void ToolBarItem::setAction(QAction *action)
{
    m_action = action;
}

////////// ToolBarAction ///////////////////////////////////////////////////////////////////////////

ToolBarAction::ToolBarAction(QAction * action, const QString & slot, QObject * parent)
    : ToolBarItem(action, parent)
{
    m_slot = slot;
}

ToolBarItem::Type ToolBarAction::type() const
{
    return Action;
}

QString ToolBarAction::slot() const
{
    return m_slot;
}

void ToolBarAction::setSlot(const QString & slot)
{
    m_slot = slot;
}

////////// ToolBarWidget ///////////////////////////////////////////////////////////////////////////

ToolBarWidget::ToolBarWidget(QWidget * widget, QObject * parent)
    : ToolBarItem(0, parent)
{
    m_widget = widget;
}

ToolBarItem::Type ToolBarWidget::type() const
{
    return Widget;
}

QWidget * ToolBarWidget::widget()
{
    return m_widget;
}

////////// SettingsTabWidget ///////////////////////////////////////////////////////////////////////

SettingsWidget::SettingsWidget(const QString & title, QWidget * parent)
    : QWidget(parent)
{
    m_title = title;
    m_isModified = false;
}

QString SettingsWidget::title() const
{
    return m_title;
}

void SettingsWidget::setTitle(const QString & title)
{
    m_title = title;
}

bool SettingsWidget::isModified()
{
    return m_isModified;
}

void SettingsWidget::setModified(bool modified)
{
    m_isModified = modified;

    if(modified)
        emit modified;
}

////////// AppEventOpenFile ////////////////////////////////////////////////////////////////////////

AppEventOpenFile::AppEventOpenFile(const QString & path, const QDateTime date)
    : AppEvent()
{
    m_path = path;
    m_date = date;
}

AppEvent::Type AppEventOpenFile::type() const
{
    return EventOpenFile;
}

QString AppEventOpenFile::path() const
{
    return m_path;
}

QDateTime AppEventOpenFile::date() const
{
    return m_date;
}

////////// ViewWidget //////////////////////////////////////////////////////////////////////////

ViewWidget::ViewWidget(QWidget *parent)
    : QWidget(parent)
{
}

void ViewWidget::connectToolBarItems(const ToolBarItemList & items)
{
    foreach(ToolBarItem * item, items)
    {
        if(item->type() == ToolBarItem::Action)
        {
            ToolBarAction * toolBarAction = static_cast<ToolBarAction *>(item);
            QString actionSlot = toolBarAction->slot();
            if(!actionSlot.isNull())
                connect(toolBarAction->action(), SIGNAL(triggered()), this, QString("1"+ actionSlot + "()").toAscii());
        }
    }
}

void ViewWidget::disconnectToolBarItems(const ToolBarItemList & items)
{
    foreach(ToolBarItem * item, items)
    {
        if(item->type() == ToolBarItem::Action)
        {
            ToolBarAction * toolBarAction = static_cast<ToolBarAction *>(item);
            QString actionSlot = toolBarAction->slot();
            if(!actionSlot.isNull())
                QObject::disconnect(toolBarAction->action(), SIGNAL(triggered()), this, QString("1"+ actionSlot + "()").toAscii());
        }
    }
}

////////// DynamicViewWidget //////////////////////////////////////////////////////////////////////////

DynamicViewWidget::DynamicViewWidget(QWidget *parent)
    : ViewWidget(parent)
{
}

void DynamicViewWidget::gotoItem(ARSExecItem * item)
{
}

void DynamicViewWidget::drawItem( const ARSExecItem * item, QPainter * painter,
                                 const QRect & rect, bool singleLine )
{
    QRect boundingRect;
    QRect itemRect(0, 0, rect.width(), rect.height());
    if(!item)
    {
        qDebug("TreeViewDelegate::drawItem: item is NULL!!!");
        return;
    }

    const QMetaObject * itemMetaObject = item->metaObject();

    painter->save();
    painter->translate(rect.x(), rect.y());

    ItemStyle * itemStyle = g_Settings.itemStyle(itemMetaObject->className());
    QList<PropertyStyle *> propertyStylesList = itemStyle->propertyStyles(ItemStyle::IncludeInherited);
    foreach(PropertyStyle * propertyStyle, propertyStylesList)
    {
        boundingRect = drawField(painter, item, propertyStyle->name(),
                                 itemRect, propertyStyle);
        itemRect.setLeft(boundingRect.right());
    }

    painter->restore();
}

QRect DynamicViewWidget::drawField( QPainter * painter, const ARSExecItem * item, const QString & propertyName,
                                    QRect rect, PropertyStyle * propertyStyle )
{
    QVariant propertyValue = item->property(qPrintable(propertyName));
    ARSDummyObject * arsObject = 0;
    TextStyle * headerStyle = g_Settings.itemStyle("ARSExecItem")->exclusiveStyle(0);
    QRect itemRect = rect;
    QRect boundingRect;
    QString propertyLabelString;
    QString propertyValueString;

    if(!propertyStyle)
        return QRect(rect.topLeft(), rect.bottomLeft());

    PropertyStyleBase * valueStyle = propertyStyle;

    propertyLabelString = propertyStyle->label();

    if(propertyStyle->hasAliasStyles())
    {
        PropertyAliasStyle * propertyAliasStyle = propertyStyle->styleForValue(propertyValue);

        if(propertyAliasStyle)
        {
            valueStyle = propertyAliasStyle;
            propertyValueString = propertyAliasStyle->alias();
        }
    }

    if(propertyValueString.isNull())
    {
        if(propertyValue.canConvert(QVariant::String))
            propertyValueString = propertyValue.toString();
        else
        {
            arsObject = ARSDummyObject::toARSObject(propertyValue);

            if(arsObject)
                propertyValueString = arsObject->name();
            else
                propertyValueString = propertyStyle->name() + ": object is null";
        }
    }

    if(valueStyle && valueStyle->visibility() != PropertyStyle::Hidden)
    {
        if(propertyStyle->visibility() == PropertyStyle::Visible)
        {
            TextStyle::adjustPainter(painter, headerStyle);
            painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop,
                              propertyLabelString + propertyStyle->separator() + " ", &boundingRect);

            itemRect.setLeft(boundingRect.right());
        }

        TextStyle::adjustPainter(painter, valueStyle);

        painter->drawText(itemRect, Qt::AlignLeft | Qt::AlignTop,
                          propertyValueString, &boundingRect);

        itemRect.setLeft(boundingRect.right() + SpacingInsideProperty);
    }

    boundingRect = QRect(rect.topLeft(), itemRect.bottomLeft());

    return boundingRect;
}

QSize DynamicViewWidget::itemVisualSize( const ARSExecItem * item, bool singleLine )
{
    QFontMetrics defaultFontMetrics(g_Settings.itemStyle("ARSExecItem")->font());
    QSize itemSize(0, defaultFontMetrics.height());
    if(!item)
    {
        qDebug("TreeViewDelegate::drawItem: item is NULL!!!");
        return QSize();
    }

    const QMetaObject * itemMetaObject = item->metaObject();
    QString propertyLabelString;
    QString propertyValueString;
    QFontMetrics fontMetrics(QFont(g_Settings.fontFamily(), g_Settings.fontSize()));
    int fontHeight = fontMetrics.height();

    if(!(item->comment().isEmpty() || singleLine))
    {
        int lineCount = item->comment().count('\n') + 1;
        itemSize.setHeight(itemSize.height() + lineCount * fontHeight);
    }

    ItemStyle * itemStyle = g_Settings.itemStyle(itemMetaObject->className());
    QList<PropertyStyle *> propertyStylesList = itemStyle->propertyStyles(ItemStyle::IncludeInherited);
    foreach(PropertyStyle * propertyStyle, propertyStylesList)
    {
        QVariant propertyValue = item->property(qPrintable(propertyStyle->name()));
        ARSDummyObject * arsObject = 0;

        PropertyStyleBase * valueStyle = propertyStyle;
        propertyValueString = QString();
        if(propertyStyle)
        {
            propertyLabelString = propertyStyle->label();

            if(propertyStyle->hasAliasStyles())
            {
                PropertyAliasStyle * propertyAliasStyle = propertyStyle->styleForValue(propertyValue);

                if(propertyAliasStyle)
                {
                    valueStyle = propertyAliasStyle;
                    propertyValueString = propertyAliasStyle->alias();
                }
            }

            if(propertyValueString.isNull())
            {
                if(propertyValue.canConvert(QVariant::String))
                    propertyValueString = propertyValue.toString();
                else
                {
                    arsObject = ARSDummyObject::toARSObject(propertyValue);

                    if(arsObject)
                        propertyValueString = arsObject->name();
                    else
                        propertyValueString = "object is null";
                }
            }

            if(valueStyle && valueStyle->visibility() != PropertyStyle::Hidden)
            {
                if(propertyStyle->visibility() == PropertyStyle::Visible)
                {
                    itemSize.setWidth(itemSize.width() +
                                      fontMetrics.boundingRect(propertyLabelString + propertyStyle->separator() + " ").width());
                }

                itemSize.setWidth(itemSize.width() + fontMetrics.boundingRect(propertyValueString).width() + SpacingInsideProperty);
            }
        }
    }

    return itemSize;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

UseCaseViewWidget::UseCaseViewWidget(QWidget *parent)
    : ViewWidget(parent)
{
}

////////// ServerViewWidget ////////////////////////////////////////////////////////////////////////

ServerViewWidget::ServerViewWidget(QWidget *parent)
    : ViewWidget(parent)
{
}


///////////////////////////////////////////////////////////////////////////////////////////////////////
